/**
 * Copyright 2009 by Christian Scheid. This file is part of the switch eclipse plugin and is licensed under the GPL
 * version 3. Please refer to the URL http://www.gnu.org/licenses/gpl-3.0.html for details.
 */
package org.digitalsoul.switchplugin.editoractions;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IMarker;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.texteditor.AbstractTextEditor;

public class MaintainSelectionEditorAction extends AbstractEditorAction {

    @Override
    public void applyAction(IFile newFile, IFile lastFile, IEditorPart newEditor, IEditorPart lastEditor) {

        ISelection selection = getSelection(lastEditor);
        if (selection != null && selection instanceof ITextSelection) {

            ITextSelection txtSelection = (ITextSelection) selection;
            AbstractTextEditor editor = (AbstractTextEditor) newEditor;
            IDocument document = editor.getDocumentProvider().getDocument(newEditor.getEditorInput());

            MatchingPosition pos = findMatch(document.get(), txtSelection.getText());
            int charBegin = pos.getCharNumber();
            int charEnd = pos.getCharNumber() + txtSelection.getText().length();
            int lineNumber = 1;

            if (pos.isMatching()) {
                String markerType = "org.eclipse.core.resources.textmarker";
                IMarker marker = null;
                try {
                    Map<String, Integer> attributes = new HashMap<String, Integer>();
                    attributes.put(IMarker.CHAR_START, charBegin);
                    attributes.put(IMarker.CHAR_END, charEnd);
                    attributes.put(IMarker.LINE_NUMBER, lineNumber);
                    newFile.deleteMarkers(markerType, true, IFile.DEPTH_INFINITE);
                    marker = newFile.createMarker(markerType);
                    marker.setAttributes(attributes);
                    IDE.gotoMarker(editor, marker);
                }
                catch (CoreException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    MatchingPosition findMatch(String documentText, String searchText) {

        int charPos = 0;
        int foundPos = -1;

        StringReader stringReader = new StringReader(documentText);
        BufferedReader reader = new BufferedReader(stringReader);
        String line = null;
        try {
            while ((line = reader.readLine()) != null && foundPos < 0) {
                foundPos = line.indexOf(searchText);
                if (foundPos > -1) {
                    charPos += foundPos;
                }
                else {
                    charPos += line.length() + 1;
                }
            }
            stringReader.close();
            reader.close();
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return new MatchingPosition(1, charPos);
    };
}
